package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;

import sgi.message.MessageRetrieveTariff.TariffRow;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SGICEOTariffUI extends SGIClientFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField tfImageCurr;
	private JTextField tfTimeLocationSubCurr;
	private JTextField tfImagePackSubCurr;
	private JTextField tfDataLayerSubCurr;
	private JTextField tfImageNew;
	private JTextField tfTimeLocationSubNew;
	private JTextField tfImagePackSubNew;
	private JTextField tfDataLayerSubNew;
	private JButton btnApprove;
	private JButton btnDecline;
	private SGICEOTariffController sgiceoTariffController;
	
	public SGICEOTariffUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 541, 318);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("SGI CEO");
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_12 = new JPanel();
		panel_1.add(panel_12, BorderLayout.EAST);
		
		btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				approveOrDecline(true);
			}
		});
		btnApprove.setEnabled(false);
		panel_12.add(btnApprove);
		
		btnDecline = new JButton("Decline");
		btnDecline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				approveOrDecline(false);
			}
		});
		btnDecline.setEnabled(false);
		panel_12.add(btnDecline);
		
		JPanel panel_13 = new JPanel();
		panel_1.add(panel_13, BorderLayout.WEST);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGICEOTariffController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		panel_13.add(btnCancel);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(3, 0, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 5, 0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5);
		
		JPanel panel_6 = new JPanel();
		panel_2.add(panel_6);
		
		JLabel lblNewLabel = new JLabel("Image");
		panel_6.add(lblNewLabel);
		
		JPanel panel_7 = new JPanel();
		panel_2.add(panel_7);
		
		JLabel lblTimeLocation = new JLabel("Time Location");
		panel_7.add(lblTimeLocation);
		
		JPanel panel_8 = new JPanel();
		panel_2.add(panel_8);
		
		JLabel lblImagePack = new JLabel("Image Pack");
		panel_8.add(lblImagePack);
		
		JPanel panel_9 = new JPanel();
		panel_2.add(panel_9);
		
		JLabel lblDataLayer = new JLabel("Data Layer");
		panel_9.add(lblDataLayer);
		
		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 5, 0, 0));
		
		JPanel panel_10 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_10.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		panel_3.add(panel_10);
		
		JLabel lblCurrentTariff = new JLabel("Current Tariff:");
		panel_10.add(lblCurrentTariff);
		
		tfImageCurr = new JTextField();
		tfImageCurr.setBackground(Color.WHITE);
		tfImageCurr.setEditable(false);
		panel_3.add(tfImageCurr);
		tfImageCurr.setColumns(10);
		
		tfTimeLocationSubCurr = new JTextField();
		tfTimeLocationSubCurr.setBackground(Color.WHITE);
		tfTimeLocationSubCurr.setEditable(false);
		panel_3.add(tfTimeLocationSubCurr);
		tfTimeLocationSubCurr.setColumns(10);
		
		tfImagePackSubCurr = new JTextField();
		tfImagePackSubCurr.setBackground(Color.WHITE);
		tfImagePackSubCurr.setEditable(false);
		panel_3.add(tfImagePackSubCurr);
		tfImagePackSubCurr.setColumns(10);
		
		tfDataLayerSubCurr = new JTextField();
		tfDataLayerSubCurr.setBackground(Color.WHITE);
		tfDataLayerSubCurr.setEditable(false);
		panel_3.add(tfDataLayerSubCurr);
		tfDataLayerSubCurr.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 5, 0, 0));
		
		JPanel panel_11 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_11.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		panel_4.add(panel_11);
		
		JLabel lblNewTariff = new JLabel("New Tariff:");
		panel_11.add(lblNewTariff);
		
		tfImageNew = new JTextField();
		tfImageNew.setBackground(Color.WHITE);
		tfImageNew.setEditable(false);
		tfImageNew.setColumns(10);
		panel_4.add(tfImageNew);
		
		tfTimeLocationSubNew = new JTextField();
		tfTimeLocationSubNew.setBackground(Color.WHITE);
		tfTimeLocationSubNew.setEditable(false);
		tfTimeLocationSubNew.setColumns(10);
		panel_4.add(tfTimeLocationSubNew);
		
		tfImagePackSubNew = new JTextField();
		tfImagePackSubNew.setBackground(Color.WHITE);
		tfImagePackSubNew.setEditable(false);
		tfImagePackSubNew.setColumns(10);
		panel_4.add(tfImagePackSubNew);
		
		tfDataLayerSubNew = new JTextField();
		tfDataLayerSubNew.setBackground(Color.WHITE);
		tfDataLayerSubNew.setEditable(false);
		tfDataLayerSubNew.setColumns(10);
		panel_4.add(tfDataLayerSubNew);
	}


	public void setSgiCEOTariffController(SGICEOTariffController tariffController) {
		this.sgiceoTariffController = tariffController;
	}


	public void loadPendingTariff(TariffRow pendingTariff) {
		tfImageNew.setText(String.valueOf(pendingTariff.getImage()));
		tfTimeLocationSubNew.setText(String.valueOf(pendingTariff.getTimeLocation()));
		tfImagePackSubNew.setText(String.valueOf(pendingTariff.getImagePack()));
		tfDataLayerSubNew.setText(String.valueOf(pendingTariff.getDataLayer()));
		
		btnApprove.setEnabled(true);
		btnDecline.setEnabled(true);
	}


	public void loadCurrentTariff(TariffRow currentTariff) {
		tfImageCurr.setText(String.valueOf(currentTariff.getImage()));
		tfTimeLocationSubCurr.setText(String.valueOf(currentTariff.getTimeLocation()));
		tfImagePackSubCurr.setText(String.valueOf(currentTariff.getImagePack()));
		tfDataLayerSubCurr.setText(String.valueOf(currentTariff.getDataLayer()));	
	}

	private void approveOrDecline(boolean approve) {
		sgiceoTariffController.approveTariff(approve);
		tfImageNew.setText("");
		tfTimeLocationSubNew.setText("");
		tfImagePackSubNew.setText("");
		tfDataLayerSubNew.setText("");
		
		btnApprove.setEnabled(false);
		btnDecline.setEnabled(false);
	}

}
